﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;

namespace VietImage.Models
{
    public partial class Action
    {
        #region Properties
        public Language Language
        {
            get
            {
                return this.GetLanguage();
            }
        }
        #endregion

        #region Methods
        public Language GetLanguage()
        {             
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = from Languages in _db.Languages
                                 where Languages.ID == this.ID_Language
                                 select Languages;
            if (v.Count() == 0)
                return null;
            return v.First();
        }
        #endregion

        #region Static Methods
        public static Action GetAction(int id, int id_language)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Actions.Where(p => p.ID == id && p.ID_Language == id_language);
            if (v.Count() == 0)
                return null;
            return v.First();
        }

        public static List<Action> GetList()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Actions.OrderByDescending(p => p.ID);
            if (v.Count() == 0)
                return null;
            return v.ToList();
        }

        private static List<ErrorItem> InputInfomationValidation(Action action)
        {
            List<ErrorItem> errorList = new List<ErrorItem>();
            if (action.ID_Language < 1)
                errorList.Add(new ErrorItem("ID_Language", "Language is required."));
            if (action.Description == "")
                errorList.Add(new ErrorItem("Description", "Description is required."));
            if (action.Description.Length > 300)
                errorList.Add(new ErrorItem("Description", "Description is required."));
            return errorList;
        }

        public static List<ErrorItem> Insert(ref Action action)
        {
            List<ErrorItem> errorList = InputInfomationValidation(action);
            if (errorList.Count == 0)
            {
                System.Object ob = (System.Object)action;
                if (!Provider.Insert(ref ob, "Actions"))
                    errorList.Add(new ErrorItem("Insert", "Failed."));
            }
            return errorList;
        }

        //public static List<ErrorItem> ChangeInformation(Action action)
        //{
        //    List<ErrorItem> errorList = InputInfomationValidation(action);
        //    if (errorList.Count == 0)
        //    {
        //        if (!Provider.Update())
        //            errorList.Add(new ErrorItem("Update", "Failed.");
        //    }
        //    return errorList;
        //}
        #endregion

    }
}